package com.guoq.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoq.constant.Configration;
import com.guoq.mapper.SmsMapper;
import com.guoq.pojo.EUDataGridResult;
import com.guoq.pojo.Result;
import com.guoq.pojo.Sms;
import com.guoq.pojo.SmsExample;
import com.guoq.pojo.SmsExample.Criteria;
import com.guoq.service.ISmsService;
import com.guoq.utils.DateConvert;
import com.guoq.utils.Strkit;

@Service("smsService")
public class SmsServiceImpl implements ISmsService {

	@Resource
	private SmsMapper smsMapper;

	@Override
	public Sms getById(int id) {
		// TODO Auto-generated method stub
		return this.smsMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public Sms getByPhonenumber(String phonenumber) {
		// TODO Auto-generated method stub
		SmsExample example = new SmsExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhonenumberEqualTo(phonenumber);
		criteria.andReceivetimeBetween(DateConvert.getTimeByMinute(-Configration.TEN), new Date());	//获取10分钟内的短信验证码
		example.setOrderByClause("receivetime desc");
		List<Sms> list = smsMapper.selectByExample(example);
		
		if(list.isEmpty()){
			return null;
		}else{
			return list.get(0);
		}
	}

	@Override
	public List<Sms> getList() {
		// TODO Auto-generated method stub
		SmsExample example = new SmsExample();
		example.setOrderByClause("id");
		List<Sms> list = smsMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public EUDataGridResult getPageList(Sms record, Integer page, Integer rows) {
		
		// TODO Auto-generated method stub
		SmsExample example = new SmsExample();
		Criteria criteria = example.createCriteria();
		
		String phonenumber = record.getPhonenumber();
		if(Strkit.notBlank(phonenumber)){
			criteria.andPhonenumberEqualTo(phonenumber);
		}
		
		example.setOrderByClause("createtime desc");
		
		//分页处理
		PageHelper.startPage(page, rows);
		List<Sms> list = smsMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<Sms> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public List<Sms> selectByExample(SmsExample example) {
		// TODO Auto-generated method stub
		List<Sms> list = smsMapper.selectByExample(example);
		return list;
	}

	@Override
	public Sms selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		Sms bean = smsMapper.selectByPrimaryKey(id);
		return bean;
	}

	@Override
	public int countByExample(SmsExample example) {
		// TODO Auto-generated method stub
		int count = smsMapper.countByExample(example);
		return count;
	}

	@Override
	public Result create(Sms record) throws Exception {

		Result result = this.insert(record);
		if(result.getStatus() != 200){
			throw new Exception();
		}
		return Result.ok();
	}
	
	/**
	 * 添加商品描述
	 *<P>开发者:郭强
	 *<P>方法名: insert
	 *<P>描述: 
	 *@param itemId
	 *@param desc
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	private Result insert(Sms record){
		
		smsMapper.insertSelective(record);
		return Result.ok();
	}

	/**
	 * 删除
	 */
	@Override
	public Result delete(Sms record) throws Exception {

		Result result = null;
		return result;
	}

	/**
	 * 编辑
	 */
	@Override
	public Result update(Sms record) throws Exception {

		Result result = null;
		return result;
	}
}
